﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Windows.Forms;

namespace Com.TomCdc.ChangeDataCapture
{
    public partial class ServerConnection : Form
    {
        /// <summary>
        /// Initializes a new instance of <see cref="ServerConnection"/> class.
        /// </summary>
        public ServerConnection()
        {
            DialogResult = DialogResult.Cancel;
            InitializeComponent();
        }


        string _serverName;
        /// <summary>
        /// The name of the server entered in the UI.
        /// </summary>
        public string ServerName
        {
            get
            {
                return _serverName;
            }
        }

        private void uxOk_Click(object sender, EventArgs e)
        {
            VerifyUiData();
        }

        private void uxCancel_Click(object sender, EventArgs e)
        {
            _serverName = "";
        }

        private void uxServerName_KeyDown(object sender, KeyEventArgs e)
        {
            // spaces not allowed in server name
            if (e.KeyCode == Keys.Space)
            {
                e.SuppressKeyPress = true;
            }
        }

        private void uxServerName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                VerifyUiData();
            }
        }

        private void VerifyUiData()
        {
            // validates server name
            if (string.IsNullOrEmpty(uxServerName.Text))
            {
                // no server name entered
                MessageBox.Show("No server name has been entered.", "Server connection",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                // assigns UI filed value to variable accessed through property when dialog closed
                _serverName = uxServerName.Text;
                DialogResult = DialogResult.OK;
                Close();
            }
        }
    }
}
